package nautilus.moneykeeper;

import java.util.Stack;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import nautilus.lib.slidinglayout.SlidingLayout;
import nautilus.lib.slidinglayout.app.SlidingFragmentActivity;
import nautilus.moneykeeper.fragment.AbstractMainContentFragment;
import nautilus.moneykeeper.fragment.HomeMenuFragment;
import nautilus.moneykeeper.fragment.ListAccountFragment;
import nautilus.moneykeeper.system.Constant;

public class MainBaseSlidingActvity extends SlidingFragmentActivity{
	private static final String EXT_BACKSTACK = "BACKSTACK";
	private static final String TAG_LEFT_MENU_FRAGMENT = "TAG_LEFT_MENU_FRAGMENT";
	private static final String TAG_RIGHT_MENU_FRAGMENT = "TAG_RIGHT_MENU_FRAGMENT";
	private static final String TAG_CONTENT_FRAGMENT = "TAG_CONTENT_FRAGMENT";
	private HomeMenuFragment _menuFragment;
	
	private Stack<String> _stackTagFragments = new Stack<String>();
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_frame_layout);
		
		setBehindContentView(R.layout.left_frame_layout);
		
		SlidingLayout slidingLayout = getSlidingLayout();
		slidingLayout.setBehindOffsetRes(R.dimen.main_left_menu_offset);
		slidingLayout.setSecondaryMenu(R.layout.right_frame_layout);
		slidingLayout.setMode(SlidingLayout.LEFT_RIGHT);
		slidingLayout.setTouchModeAbove(SlidingLayout.TOUCHMODE_NONE);
		slidingLayout.setBehindScrollScale(0f);
		
		//Load default fragment
		_menuFragment = new HomeMenuFragment();
		setLeftFragment(_menuFragment);
		
		Fragment accountsFragment;
		if(savedInstanceState != null ){
			
			/**
			 * THERE IS SOMETHING NOT GOOD HERE, SOMETIME IT'S CONSIDER THAT EXT_BACKSTACK AS A LIST NOT STACK
			 * */
			String tags = savedInstanceState.getString(EXT_BACKSTACK);
			String[] tagItems;
			if(tags != null){
				tagItems = tags.split("@");
				
				for(int i=tagItems.length-1; i>=0; i--){
					_stackTagFragments.push(tagItems[i]);
				}
			}
			//_stackTagFragments = (Stack<String>)savedInstanceState.getSerializable(EXT_BACKSTACK);
			//android.util.Log.i("MainBaseActivity", className);
			//accountsFragment = getFragmentFromTag(className);
			//setMainFragment(accountsFragment, 0, 0);
			return;
		}
		
		//ListAccountFragment will be considered as MainHomeFragment
		accountsFragment = new ListAccountFragment();
		setMainFragment(accountsFragment, 0, 0);
	}
	
//	@Override
//	public void onRestoreInstanceState(Bundle inState){
//		android.util.Log.i("MainBaseActivity", "Class name: " + inState.getString(Constant.CLASS_NAME));
//		super.onRestoreInstanceState(inState);
//	}
	
	@Override
	public void onSaveInstanceState(Bundle outState){
		super.onSaveInstanceState(outState);
		outState.putString(Constant.CLASS_NAME, getHomeFragment().getClass().getName());
		
		//outState.putSerializable(EXT_BACKSTACK, _stackTagFragments);
		String tags = "";
		String item;
		while(!_stackTagFragments.isEmpty()){
			item = _stackTagFragments.pop();
			if(tags.length()<=0)
				tags = item;
			else
				tags += "@" + item;
		}
		outState.putString(EXT_BACKSTACK, tags);
	}
	
	@Override
	public void onBackPressed(){
		if(_stackTagFragments.size()<=1){
			super.onBackPressed();
			return;
		}
		((AbstractMainContentFragment)getHomeFragment()).onBackPressed();
	}
	
	public void postBackPressed(Bundle args){
		//_stackTagFragments.pop();
		String current = _stackTagFragments.pop();
		android.util.Log.i("MainBaseSliding", current);
		Fragment previousFragment = getFragmentFromTag(_stackTagFragments.peek());
		if(previousFragment == null){
			super.onBackPressed();
			return;
		}
		
		previousFragment.setArguments(args);
		FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
		trans.setCustomAnimations(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
		trans.replace(R.id.main_frame_layout_content, previousFragment, TAG_CONTENT_FRAGMENT);
		trans.show(previousFragment);
		trans.commit();
	}
	
	private Fragment getFragmentFromTag(String className){
		try {
			Class<?> cls = Class.forName(className);
			Fragment fg = (Fragment)cls.newInstance();
			return fg;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private Fragment getFragmentFromClass(Class<?> cls){
		try {
			Fragment fg = (Fragment)cls.newInstance();
			return fg;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public void setLeftFragment(Fragment frag){
		FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
		trans.replace(R.id.left_frame_layout, frag, TAG_LEFT_MENU_FRAGMENT);
		trans.commit();
	}
	
	public void setRightFragment(Fragment frag){
		FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
		trans.replace(R.id.right_frame_layout, frag, TAG_RIGHT_MENU_FRAGMENT);
		trans.commit();
	}
	
	public void toHomeWithoutStack(int inAnimation, int outAnimation, Bundle args){
		String seekClassName = _stackTagFragments.peek();
		String homeClassName = ListAccountFragment.class.getName();
		
		while(!seekClassName.equals(homeClassName)){
			_stackTagFragments.pop();
			seekClassName = _stackTagFragments.peek();
		}
		
		Fragment frag = new ListAccountFragment();
		frag.setArguments(args);
		FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
		if(inAnimation>0 || outAnimation>0)
			trans.setCustomAnimations(inAnimation, outAnimation);
		trans.replace(R.id.main_frame_layout_content, frag, TAG_CONTENT_FRAGMENT);
		trans.show(frag);
		trans.commit();
	}
	
	public void setMainFragment(Fragment frag, int inAnimation, int outAnimation){
		String tag = frag.getClass().getName();
		_stackTagFragments.push(tag);
		FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
		if(inAnimation>0 || outAnimation>0)
			trans.setCustomAnimations(inAnimation, outAnimation);
		trans.replace(R.id.main_frame_layout_content, frag, TAG_CONTENT_FRAGMENT);
		trans.show(frag);
		trans.commit();
	}
	
	public void setMainFragment(String classFragment, int inAnimation, int outAnimation, Bundle args){
		_stackTagFragments.push(classFragment);
		Fragment frag = getFragmentFromTag(classFragment);
		frag.setArguments(args);
		FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
		if(inAnimation>0 || outAnimation>0)
			trans.setCustomAnimations(inAnimation, outAnimation);
		trans.replace(R.id.main_frame_layout_content, frag, TAG_CONTENT_FRAGMENT);
		trans.show(frag);
		trans.commit();
	}
	
	public void setMainFragment(Class<?> classFragment, int inAnimation, int outAnimation, Bundle args){
		_stackTagFragments.push(classFragment.getName());
		Fragment frag = getFragmentFromClass(classFragment);
		frag.setArguments(args);
		FragmentTransaction trans = this.getSupportFragmentManager().beginTransaction();
		if(inAnimation>0 || outAnimation>0)
			trans.setCustomAnimations(inAnimation, outAnimation);
		trans.replace(R.id.main_frame_layout_content, frag, TAG_CONTENT_FRAGMENT);
		trans.show(frag);
		trans.commit();
	}
	
	public Fragment getLeftFragment() {
		return getSupportFragmentManager().findFragmentByTag(TAG_LEFT_MENU_FRAGMENT);
	}
	
	public Fragment getRightFragment() {
		return getSupportFragmentManager().findFragmentByTag(TAG_RIGHT_MENU_FRAGMENT);
	}
	
	public Fragment getHomeFragment() {
		return getSupportFragmentManager().findFragmentByTag(TAG_CONTENT_FRAGMENT);
	}
	
	public void openLeftMenu() {
		getSlidingLayout().toggle(true);
	}

	public void openRightMenu() {
		getSlidingLayout().showSecondaryMenu(true);
	}
	
	public void closeMenu() {
		getSlidingLayout().showContent(false);
	}
	
	public void onMenuItemClicked(View v){
		_menuFragment.onMenuItemClicked(v);
	}
	
	public void onMainContentClicked(View v){
		AbstractMainContentFragment fg = (AbstractMainContentFragment) getHomeFragment();
		fg.onClick(v);
	}
}
